package com.sb.callback;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sb.crawler.impl.ZheJiangZW;
import com.sb.entity.User;
import com.sb.utils.Utils;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * Created by wujunzhou on 2017/11/2.
 */
public class CallBack {

    private static final Logger logger = LoggerFactory.getLogger(CallBack.class);

    public static boolean loginSuccess(User user, String type) {
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet("https://datamore.guoshujinfu.com/sparta/html/socialinsurance/callback?sessionId=" + user.getSessionId() + "&token=" + Utils.urlEncode(user.getToken(),"utf-8") + "&type=" + type + "&status=SUCCESS");
            logger.info("checkcode url : =======");
            logger.info(httpGet.getURI().toString());
            logger.info("=======");
            final CloseableHttpResponse response = client.execute(httpGet);
            final int code = response.getStatusLine().getStatusCode();
            if (code == HttpStatus.SC_OK) {
                String result = new String(EntityUtils.toString(response.getEntity(), "gbk"));
                if ("SUCCESS".equals(result))
                return true;
            }
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage(), e);
        } catch (ClientProtocolException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return false;
    }

    /**
     * 解析 json post
     * @param json
     * @return
     */
    public static boolean postData(String json) {
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            String url = "https://datamore.guoshujinfu.com/sparta/api/socialinsurancesave";
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
            logger.info("checkcode url : =======");
            logger.info(httpPost.getURI().toString());
            logger.info("=======");
            StringEntity se = new StringEntity(json,"utf-8");
            se.setContentType("text/json");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httpPost.setEntity(se);
            final CloseableHttpResponse response = client.execute(httpPost);
            final int code = response.getStatusLine().getStatusCode();
            if (code == HttpStatus.SC_OK) {
                String result = new String(EntityUtils.toString(response.getEntity(), "gbk"));
                logger.info("发送成功:"+result);
                if (result.contains("成功"))
                    return true;
            }
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage(), e);
        } catch (ClientProtocolException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return false;
    }

    public static void main(String[] args) {
        postData("{\"account\":{\"userId\":\"111\",\"userSource\":\"Daily\",\"sessionId\":\"3\",\"customerNo\":\"3****************9\",\"name\":\"*君舟\",\"gender\":\"男\",\"address\":\"杭州市本级\",\"medicareBalance\":\"347117\",\"flows\":[{\"standardName\":\"养老\",\"paymentMonth\":\"201710\",\"paymentBase\":\"281925\",\"personAmount\":\"22554\",\"accountFlag\":\"未到账\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201708\",\"paymentBase\":\"580000\",\"personAmount\":\"46400\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201707\",\"paymentBase\":\"580000\",\"personAmount\":\"46400\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201706\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201706\",\"paymentBase\":\"6667\",\"personAmount\":\"533\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201705\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201705\",\"paymentBase\":\"6667\",\"personAmount\":\"533\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201704\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201704\",\"paymentBase\":\"6667\",\"personAmount\":\"533\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201703\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201703\",\"paymentBase\":\"6667\",\"personAmount\":\"533\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201702\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201702\",\"paymentBase\":\"6667\",\"personAmount\":\"533\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201701\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201701\",\"paymentBase\":\"6667\",\"personAmount\":\"533\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201612\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201611\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201610\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201609\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201608\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201607\",\"paymentBase\":\"573333\",\"personAmount\":\"45867\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201606\",\"paymentBase\":\"453300\",\"personAmount\":\"36264\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201606\",\"paymentBase\":\"120033\",\"personAmount\":\"9603\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201605\",\"paymentBase\":\"453300\",\"personAmount\":\"36264\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201605\",\"paymentBase\":\"120033\",\"personAmount\":\"9603\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201604\",\"paymentBase\":\"453300\",\"personAmount\":\"36264\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201604\",\"paymentBase\":\"120033\",\"personAmount\":\"9603\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201603\",\"paymentBase\":\"453300\",\"personAmount\":\"36264\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201603\",\"paymentBase\":\"120033\",\"personAmount\":\"9603\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201602\",\"paymentBase\":\"453300\",\"personAmount\":\"36264\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201602\",\"paymentBase\":\"120033\",\"personAmount\":\"9603\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201601\",\"paymentBase\":\"453300\",\"personAmount\":\"36264\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201601\",\"paymentBase\":\"120033\",\"personAmount\":\"9603\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201512\",\"paymentBase\":\"453300\",\"personAmount\":\"36264\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"养老\",\"paymentMonth\":\"201511\",\"paymentBase\":\"453300\",\"personAmount\":\"36264\",\"accountFlag\":\"已实缴\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201710\",\"paymentBase\":\"281925\",\"personAmount\":\"6039\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201708\",\"paymentBase\":\"580000\",\"personAmount\":\"12000\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201707\",\"paymentBase\":\"580000\",\"personAmount\":\"12000\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201706\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201706\",\"paymentBase\":\"6667\",\"personAmount\":\"133\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201705\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201705\",\"paymentBase\":\"6667\",\"personAmount\":\"133\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201704\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201704\",\"paymentBase\":\"6667\",\"personAmount\":\"133\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201703\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201703\",\"paymentBase\":\"6667\",\"personAmount\":\"133\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201702\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201702\",\"paymentBase\":\"6667\",\"personAmount\":\"133\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201701\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201701\",\"paymentBase\":\"6667\",\"personAmount\":\"133\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201612\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201611\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201610\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201609\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201608\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201607\",\"paymentBase\":\"573333\",\"personAmount\":\"11867\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201606\",\"paymentBase\":\"453300\",\"personAmount\":\"9466\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201606\",\"paymentBase\":\"120033\",\"personAmount\":\"2401\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201605\",\"paymentBase\":\"453300\",\"personAmount\":\"9466\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201605\",\"paymentBase\":\"120033\",\"personAmount\":\"2401\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201604\",\"paymentBase\":\"453300\",\"personAmount\":\"9466\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201604\",\"paymentBase\":\"120033\",\"personAmount\":\"2401\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201603\",\"paymentBase\":\"453300\",\"personAmount\":\"9466\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201603\",\"paymentBase\":\"120033\",\"personAmount\":\"2401\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201602\",\"paymentBase\":\"453300\",\"personAmount\":\"9466\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201602\",\"paymentBase\":\"120033\",\"personAmount\":\"2401\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201601\",\"paymentBase\":\"453300\",\"personAmount\":\"9466\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201601\",\"paymentBase\":\"120033\",\"personAmount\":\"2401\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201512\",\"paymentBase\":\"453300\",\"personAmount\":\"9466\"},{\"standardName\":\"医疗\",\"paymentMonth\":\"201511\",\"paymentBase\":\"453300\",\"personAmount\":\"9466\"}],\"insurances\":[{\"standardName\":\"养老\",\"insuredStatus\":\"参保缴费\",\"company\":\"浙江普树信息科技有限公司\"},{\"standardName\":\"工伤\"},{\"standardName\":\"医疗\",\"insuredStatus\":\"参保缴费\",\"company\":\"浙江普树信息科技有限公司\"},{\"standardName\":\"失业\",\"insuredStatus\":\"参保缴费\",\"company\":\"浙江普树信息科技有限公司\"},{\"standardName\":\"生育\",\"insuredStatus\":\"参保缴费\",\"company\":\"浙江普树信息科技有限公司\"}],\"medicalConsume\":[{\"consumesMonth\":\"2017-10-31\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"1000\",\"cashPayment\":\"0\",\"accountPayment\":\"1000\",\"medicalInstitutionName\":\"西湖区蒋村街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-10-31\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"1500\",\"cashPayment\":\"0\",\"accountPayment\":\"1500\",\"medicalInstitutionName\":\"西湖区蒋村街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-10-30\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"1000\",\"cashPayment\":\"0\",\"accountPayment\":\"1000\",\"medicalInstitutionName\":\"西湖区蒋村街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-10-30\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"1500\",\"cashPayment\":\"0\",\"accountPayment\":\"1500\",\"medicalInstitutionName\":\"西湖区蒋村街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-10-29\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"4518\",\"cashPayment\":\"0\",\"accountPayment\":\"4518\",\"medicalInstitutionName\":\"拱墅区康桥街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-10-29\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"0\",\"cashPayment\":\"0\",\"accountPayment\":\"0\",\"medicalInstitutionName\":\"拱墅区康桥街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-10-28\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"2500\",\"cashPayment\":\"0\",\"accountPayment\":\"2500\",\"medicalInstitutionName\":\"拱墅区康桥街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-10-28\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"0\",\"cashPayment\":\"0\",\"accountPayment\":\"0\",\"medicalInstitutionName\":\"拱墅区康桥街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-10-28\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"1500\",\"cashPayment\":\"0\",\"accountPayment\":\"1500\",\"medicalInstitutionName\":\"杭州标卓投资管理有限公司康桥诊所\"},{\"consumesMonth\":\"2017-10-27\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"1000\",\"cashPayment\":\"0\",\"accountPayment\":\"1000\",\"medicalInstitutionName\":\"西湖区蒋村街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-10-27\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"2598\",\"cashPayment\":\"0\",\"accountPayment\":\"2598\",\"medicalInstitutionName\":\"西湖区蒋村街道社区卫生服务中心\"},{\"consumesMonth\":\"2017-03-28\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"21800\",\"cashPayment\":\"0\",\"accountPayment\":\"21800\",\"medicalInstitutionName\":\"杭州九洲大药房连锁有限公司(总部）\"},{\"consumesMonth\":\"2017-03-25\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"7120\",\"cashPayment\":\"0\",\"accountPayment\":\"7120\",\"medicalInstitutionName\":\"杭州九洲大药房连锁有限公司(总部）\"},{\"consumesMonth\":\"2017-03-25\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"4050\",\"cashPayment\":\"0\",\"accountPayment\":\"4050\",\"medicalInstitutionName\":\"杭州九洲大药房连锁有限公司(总部）\"},{\"consumesMonth\":\"2017-03-17\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"10420\",\"cashPayment\":\"0\",\"accountPayment\":\"10420\",\"medicalInstitutionName\":\"杭州九洲大药房连锁有限公司(总部）\"},{\"consumesMonth\":\"2017-03-17\",\"consumesType\":\"普通门诊\",\"consumesAmount\":\"16660\",\"cashPayment\":\"0\",\"accountPayment\":\"16660\",\"medicalInstitutionName\":\"杭州海王星辰健康药房有限公司(总部）\"}],\"endowmentAccount\":[{\"year\":\"2016\",\"lastYearBalance\":\"867619\",\"paymentMonths\":\"12\",\"paymentAmount\":\"550404\",\"interest\":\"23379\",\"balance\":\"1441402\",\"totalMonths\":\"44\"},{\"year\":\"2015\",\"lastYearBalance\":\"359446\",\"paymentMonths\":\"12\",\"paymentAmount\":\"435168\",\"interest\":\"6438\",\"balance\":\"801052\",\"totalMonths\":\"32\"},{\"year\":\"2014\",\"lastYearBalance\":\"130814\",\"paymentMonths\":\"12\",\"paymentAmount\":\"213660\",\"interest\":\"3624\",\"balance\":\"348098\",\"totalMonths\":\"20\"},{\"year\":\"2013\",\"lastYearBalance\":\"13266\",\"paymentMonths\":\"7\",\"paymentAmount\":\"112245\",\"interest\":\"1173\",\"balance\":\"126684\",\"totalMonths\":\"8\"},{\"year\":\"2012\",\"lastYearBalance\":\"12848\",\"paymentMonths\":\"0\",\"paymentAmount\":\"0\",\"interest\":\"0\",\"balance\":\"12848\",\"totalMonths\":\"1\"},{\"year\":\"2011\",\"lastYearBalance\":\"0\",\"paymentMonths\":\"1\",\"paymentAmount\":\"12260\",\"interest\":\"168\",\"balance\":\"12428\",\"totalMonths\":\"1\"}]}}");
    }
}
